$grid-columns: 12;

.grid-container {
  @include overflow(hidden);
}

@mixin make-cell($columns) {
  .cell--#{$columns} {
    @if $columns == "auto" {
      @include flex(1 1 0);
      width: auto;
      min-width: 0;
    } @else if $columns == "shrink" {
      @include flex(0 0 auto);
      width: auto;
      min-width: 0;
    } @else if $columns == "stretch" {
      @include flex(1);
    } @else {
      @include flex(none);
      width: percentage($columns / $grid-columns);
      min-width: 0;
    }
  }
}

@mixin make-md-cell($columns) {
  @include media-breakpoint-down(lg) {
    .cell--md-#{$columns} {
      @if $columns == "auto" {
        @include flex(1 1 0);
        width: auto;
        min-width: 0;
      } @else if $columns == "shrink" {
        @include flex(0 0 auto);
        width: auto;
        min-width: 0;
      } @else if $columns == "stretch" {
        @include flex(1);
      } @else {
        @include flex(none);
        width: percentage($columns / $grid-columns);
        min-width: 0;
      }
    }
  }
}

@mixin make-sm-cell($columns) {
  @include media-breakpoint-down(md) {
    .cell--sm-#{$columns} {
      @if $columns == "auto" {
        @include flex(1 1 0);
        width: auto;
        min-width: 0;
      } @else if $columns == "shrink" {
        @include flex(0 0 auto);
        width: auto;
        min-width: 0;
      } @else if $columns == "stretch" {
        @include flex(1);
      } @else {
        @include flex(none);
        width: percentage($columns / $grid-columns);
        min-width: 0;
      }
    }
  }
}

.grid {
  @include flexbox();
  @include flex-wrap(wrap);
  & > {
    @for $i from 1 through $grid-columns {
      @include make-cell($i);
    }
    @include make-cell("auto");
    @include make-cell("shrink");
    @include make-cell("stretch");

    @for $i from 1 through $grid-columns {
      @include make-md-cell($i);
    }
    @include make-md-cell("auto");
    @include make-md-cell("shrink");
    @include make-md-cell("stretch");

    @for $i from 1 through $grid-columns {
      @include make-sm-cell($i);
    }

    @include make-sm-cell("auto");
    @include make-sm-cell("shrink");
    @include make-sm-cell("stretch");
  }
}

.grid--reverse {
  flex-direction: row-reverse;
}

@mixin make-grid() {
  $types: ("p");
  $directions: ("x", "y", "");
  $spacers: (0, 1, 2, 3, 4, 5);

  @each $type in $types {
    @each $direction in $directions {
      @each $spacer in $spacers {
        @if $direction == "" {
          .grid--#{$type}-#{$spacer} {
            @include make-spacing("m", "", $spacer, true);
            .cell {
              @include make-spacing($type, "", $spacer);
            }
          }
        } @else {
          .grid--#{$type}#{$direction}-#{$spacer} {
            @include make-spacing("m", $direction, $spacer, true);
            .cell {
              @include make-spacing($type, $direction, $spacer);
            }
          }
        }
      }
    }
  }
}

@include make-grid();